//
//  BKSoloNavigationController.m
//  XPhoto
//
//  Created by baboy on 2019/8/28.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "BKSoloNavigationController.h"
#import "NSArray+x.h"
#import "ThemeFactory.h"

@interface BKSoloInternalNavigationController : BKNavigationController

@end
@implementation BKSoloInternalNavigationController
- (void)viewDidLoad{
    [super viewDidLoad];
    self.interactivePopGestureRecognizer.enabled = NO;
}
- (void)popViewControllerAnimated{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (self.navigationController) {
        [self.navigationController pushViewController:viewController animated:animated];
        if ([self.navigationController.viewControllers count] > 1 && !viewController.navigationController.navigationBar.hidden) {
            //
            viewController.navigationItem.leftBarButtonItem = [ThemeFactory navBarButtonForKey:@"navigationbar-back-button" withTarget:self action:@selector(popViewControllerAnimated)];
        }
    }
    if(!self.navigationController){
        [super pushViewController:viewController animated:animated];
    }
}
- (UIViewController *)popViewControllerAnimated:(BOOL)animated{
    if (self.navigationController) {
        return [self.navigationController popViewControllerAnimated:animated];
    }
    return [super popViewControllerAnimated:animated];
}
- (nullable NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (self.navigationController) {
        return [self.navigationController popToViewController:viewController animated:animated];
    }
    return [super popToViewController:viewController animated:animated];
}
- (nullable NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated{
    if (self.navigationController) {
        return [self.navigationController popToRootViewControllerAnimated:animated];
    }
    return [super popToRootViewControllerAnimated:animated];
}
- (UIViewController *)childViewControllerForScreenEdgesDeferringSystemGestures{
    return self.topViewController;
}
- (UIRectEdge)preferredScreenEdgesDeferringSystemGestures{
    return [self.topViewController preferredScreenEdgesDeferringSystemGestures];
}
- (void)setViewControllers:(NSArray<UIViewController *> *)viewControllers animated:(BOOL)animated{
    if (self.navigationController) {
        [self.navigationController setViewControllers:viewControllers animated:animated];
        return;
    }
    [super setViewControllers:viewControllers animated:animated];
}
- (void)setDelegate:(id<UINavigationControllerDelegate>)delegate{
    if(self.navigationController){
        [self.navigationController setDelegate:delegate];
        return;
    }
    [super setDelegate:delegate];
}
@end

/********************/

@interface BKSoloInternalViewController : BKViewController
@property (nonatomic, strong) __kindof UIViewController *contentViewController;
@property (nonatomic, strong) UINavigationController *internalNavigationController;

+ (instancetype)controllerWithContentViewController:(UIViewController *)contentViewController;
+ (id)contentViewController:(UIViewController *)contentViewController;
- (id)initWithContentViewController:(UIViewController *)contentViewController;
@end
//
//static inline UIViewController *BKSoloWrapViewController(UIViewController *controller) {
//    if ([controller isKindOfClass:[BKSoloInternalViewController class]]) {
//        return (BKSoloInternalViewController *)controller;
//    }
//    return [BKSoloInternalViewController controllerWithContentViewController:controller];
//}
//static inline UIViewController *BKSoloUnWrapViewController(UIViewController *controller) {
//    if ([controller isKindOfClass:[BKSoloInternalViewController class]]) {
//        return (BKSoloInternalViewController *)controller;
//    }
//    return [BKSoloInternalViewController controllerWithContentViewController:controller];
//}

@implementation BKSoloInternalViewController

+ (instancetype)controllerWithContentViewController:(UIViewController *)contentViewController{
    if([contentViewController isKindOfClass:[BKSoloInternalViewController class]]){
        return (BKSoloInternalViewController*)contentViewController;
    }
    return [[self alloc] initWithContentViewController:contentViewController];
}
- (id)initWithContentViewController:(UIViewController *)contentViewController{
    if (self = [self init]) {
        self.contentViewController = contentViewController;
        self.internalNavigationController = [[BKSoloInternalNavigationController alloc] init];
        self.internalNavigationController.viewControllers = @[contentViewController];
        [self addChildViewController:self.internalNavigationController];
        [self.internalNavigationController didMoveToParentViewController:self];
    }
    return self;
}
+ (id)contentViewController:(UIViewController *)internalViewController{
    if ([internalViewController isKindOfClass:[self class]]) {
        return [(BKSoloInternalViewController*)internalViewController contentViewController];
    }
    return internalViewController;
}
- (void)viewDidLoad{
    [super viewDidLoad];
    if (self.internalNavigationController) {
        self.internalNavigationController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        self.internalNavigationController.view.frame = self.view.bounds;
        [self.view addSubview:self.internalNavigationController.view];
//        self.internalNavigationController.view.layer.borderColor = [UIColor redColor].CGColor;
//        self.internalNavigationController.view.layer.borderWidth = 2;
        self.internalNavigationController.view.frame = self.view.bounds;
    }
    if (!self.internalNavigationController) {
        self.contentViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        self.contentViewController.view.frame = self.view.bounds;
        [self.view addSubview:self.contentViewController.view];
        self.internalNavigationController.view.frame = self.view.bounds;
    }
}
- (UIViewController *)childViewControllerForScreenEdgesDeferringSystemGestures{
    return self.contentViewController;
}
- (UIRectEdge)preferredScreenEdgesDeferringSystemGestures{
    return [self.contentViewController preferredScreenEdgesDeferringSystemGestures];
}
- (UINavigationItem *)navigationItem{
    return [self.contentViewController navigationItem];
}
@end
/********************/

@interface BKSoloNavigationController ()

@end

@implementation BKSoloNavigationController
- (instancetype)initWithRootViewController:(UIViewController *)rootViewController{
    BKSoloInternalViewController *internalViewController = [BKSoloInternalViewController controllerWithContentViewController:rootViewController];
    if (self = [super initWithRootViewController:internalViewController]) {
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [super setNavigationBarHidden:YES animated:NO];
    // Do any additional setup after loading the view.
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    BKSoloInternalViewController *internalViewController = [BKSoloInternalViewController controllerWithContentViewController:viewController];
    if (self.viewControllers.count > 0) {
        internalViewController.hidesBottomBarWhenPushed = YES;
        [super pushViewController:internalViewController animated:animated];
        return;
    }
    [super pushViewController:internalViewController animated:animated];
}
- (UIViewController *)popViewControllerAnimated:(BOOL)animated{
    BKSoloInternalViewController *internalViewController = (BKSoloInternalViewController *)[super popViewControllerAnimated:animated];
    return [BKSoloInternalViewController contentViewController:internalViewController];
}
- (nullable NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated{
    __block UIViewController *targetController = nil;
    [[super viewControllers] enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        id contentViewController = [BKSoloInternalViewController contentViewController:obj];
        if (contentViewController == viewController) {
            targetController = obj;
            *stop = YES;
        }
    }];
    if (targetController) {
        NSArray *vcs = [super popToViewController:targetController animated:animated];
        return [vcs makeObjectsWithBlock:^id(id obj, NSUInteger index) {
            return [BKSoloInternalViewController contentViewController:obj];
        }];
    }
    return nil;
}
- (nullable NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated{
    
    NSArray *vcs = [super popToRootViewControllerAnimated:animated];
    return [vcs makeObjectsWithBlock:^id(id obj, NSUInteger index) {
        return [BKSoloInternalViewController contentViewController:obj];
    }];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
- (void)setViewControllers:(NSArray<UIViewController *> *)viewControllers animated:(BOOL)animated{
    NSArray *vcs = [viewControllers makeObjectsWithBlock:^id(id obj, NSUInteger index) {
        BKSoloInternalViewController *internalViewController = [BKSoloInternalViewController controllerWithContentViewController:obj];
        return internalViewController;
    }];
    [super setViewControllers:vcs animated:animated];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return (gestureRecognizer == self.interactivePopGestureRecognizer);
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    DLOG(@"--");
    return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
